<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/4/27
 * Time: 11:07
 */
require('../extend/phpMQTT/phpMQTT.php');
//require('../extend/fast/Pin.php');

class Sub{
    protected $db = '';
    protected $car = [];
    protected $pinyin = '';
    public function __construct()
    {
        //$this->pinyin = new \Pin();

//        $dbc = array(
//            'hostname' => '127.0.0.1',
//            'username' => 'root',
//            'password' => 'root',
//            'database' => 'school'
//        );
        $dbc = array(
            'hostname' => '180.97.75.136',
            'username' => 's',
            'password' => 'wTyw3pwLDtsTGEfC',
            'database' => 's'
        );

        $mysqlnd = function_exists('mysqli_fetch_all');

        if ($mysqlnd) {
            $database = $dbc['database'];
            $db_hostname = $dbc['hostname'];
            $db_username = $dbc['username'];
            $db_password = $dbc['password'];
            $dsn = "mysql:dbname=$database;host=$db_hostname";
            $db = new PDO($dsn, $db_username, $db_password);
            $db->setAttribute(\PDO::ATTR_EMULATE_PREPARES, 0);
            $this->db = $db;
            $res = $db->query("SELECT `IMEI`,`pinyin` FROM `fa_car` WHERE ( IMEI!='' )");

            while ($row = $res -> fetch(PDO::FETCH_ASSOC)){
                $this->car[$row['IMEI']] = $row['pinyin'];
            }

        }
        else
        {
            echo '连接失败';exit;
        }
    }

    public function connect($client_id)
    {
        $server = 'develop.rltest.cn';     // change if necessary
        $port = 1883;                     // change if necessary

        //$client_id = $client_id; // make sure this is unique for connecting to sever - you could use uniqid()
        //Loader::import('phpmqtt/phpMQTT', EXTEND_PATH);

        $mqtt = new \phpmqtt\phpMQTT($server, $port, $client_id);

        $mqtt->debug = true;
        return $mqtt;
    }

    //获取主动位置
    public function sub()
    {

        $client_id = time().rand();
        $mqtt = $this->connect($client_id);
        $username = 'rl517';                   // set your username
        $password = 'rlian2022';                   // set your password
        if(!$mqtt->connect(true, NULL, $username, $password)) {
            echo '链接失败';exit;
        }

        $topics_name = "ScBusTem/DevRegularInfo";
        //$topics['ScBusTem/DevRegularInfo'] = array('qos' => 0, 'function' => 'procMsg');
        $topics[$topics_name] = array("qos" => 0, "function" =>array($this,"onMessage"));
        $mqtt->subscribe($topics, 0);

        //$nowtime=time();
        while($mqtt->proc()) {
            /*global $payload;
            if(empty($payload)){

                echo $payload;  //订阅到的消息
                continue;
            }*/
            /* if($nowtime==(time()-2)){
                 break;
             }*/
        }

        $mqtt->close();
    }

    /**
     * 在此处接MQtt的信息 进行业务处理
     * @param $topic
     * @param $msg
     */
    function onMessage($topic,$msg){
        //echo 'Msg Recieved: ' . date('r') . "\n";
        // echo "Topic: {$topic}\n\n";
        echo $msg;
        if($msg)
        {
            $msgs = json_decode($msg,true);
            if(isset($this->car[$msgs['IMEI']]))
            {
                $msgs['DTIME'] = date('Y-m-d',$msgs['TIME']);
                //$tableName = 'fa_car_'.$this->pinyin::utf8_to($this->car[$msgs['IMEI']]);
                $tableName = 'fa_car_'.$this->car[$msgs['IMEI']];
                $sql = "INSERT INTO {$tableName} (IMEI,CSQ,GPS_LNG,GPS_LAT,SPEED,SYS_VER,TEMP,HUM,INIT,TIME,DTIME) VALUES ('{$msgs['IMEI']}','{$msgs['CSQ']}','{$msgs['GPS_LNG']}','{$msgs['GPS_LAT']}','{$msgs['SPEED']}','{$msgs['SYS_VER']}','{$msgs['TEMP']}','{$msgs['HUM']}','{$msgs['INIT']}','{$msgs['TIME']}','{$msgs['DTIME']}')";
                //echo $sql."\n\n";
                $this->db->exec($sql);

            }
        }


    }

}

$a = new Sub();
$a->sub();



